package com.just.fun.arithmetic;

/**
 * 斐波那契数列
 * 0, 1, 1, 2, 3, 5, 8, 13
 * 从第三项开始，每一项都等于前两项之和
 */
public class Fibonacci {

    public static void main(String[] args) {
        System.out.println(findNFi(1));
        System.out.println(findNFi(3));
        System.out.println(findNFi(7));

        System.out.println("跳青蛙问题");
        System.out.println(jumpFloor(1));
        System.out.println(jumpFloor(3));
        System.out.println(jumpFloor(4));
    }

    public static int findNFi(int n){
        if(n<=2){
            return n-1;
        }
        int first=0;
        int second=1;
        int nNum=0;
        for (int i = 2; i < n; i++) {
            nNum=first+second;
            first=second;
            second=nNum;
        }
        return nNum;
    }

    /**
     * 跳青蛙问题
     * 一次可以跳1阶或2阶，跳N层有多少种解法
     * 1,2,3,5,8
     * @return
     */
    public static int jumpFloor(int n){
      if(n<=3){
          return n;
      }
      int first=2;
      int second=3;
      int nNum=0;
        for (int i = 3; i < n; i++) {
            nNum=first+second;
            first=second;
            second=nNum;
        }
        return nNum;
    }
}
